﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// 通用扩展
    /// </summary>
    public static class ExtExtension
    {
        /// <summary>
		/// 判断目标对象是否为空值(处理了Nothing、DBNull、String.Empty、DateEmpty、枚举器.Count=0的情况)。
		/// </summary>
		/// <param name="value">需要判断的目标对象。</param>
		/// <param name="whiteSpace">是否包含空白字符的判断。</param>
		public static bool GIsEmpty<T>(this T value, bool whiteSpace = false)
        {
            if (value == null)
            {
                return true;
            }
            if (value is DBNull)
            {
                return true;
            }
            if (value is DateTime d)
            {
                return d == (DateTime?)null;
            }
            string text = value as string;
            if (text != null)
            {
                if (!whiteSpace)
                {
                    return string.IsNullOrEmpty(text);
                }
                return string.IsNullOrWhiteSpace(text);
            }
            else
            {
                IEnumerable enumerable = value as IEnumerable;
                if (enumerable != null)
                {
                    IEnumerator expr_78 = enumerable.GetEnumerator();
                    expr_78.Reset();
                    return !expr_78.MoveNext();
                }
                string value2 = value.ToString();
                if (!whiteSpace)
                {
                    return string.IsNullOrEmpty(value2);
                }
                return string.IsNullOrWhiteSpace(value2);
            }
        }

        /// <summary>
        /// 判断目标对象是否不为空值(处理了Nothing、DBNull、String.Empty、DateEmpty、枚举器.Count=0的情况)。
        /// </summary>
        /// <param name="value">需要判断的目标对象。</param>
        /// <param name="whiteSpace">是否包含空白字符的判断。</param>
        public static bool GIsNotEmpty<T>(this T value, bool whiteSpace = false)
        {
            return !value.GIsEmpty(whiteSpace);
        }
    }
}
